#include <REGX52.H>
#include "OneWire.h"

#define DS18B20_SIKP_ROM         0xCC
#define DS18B20_CONVERT_T        0x44
#define DS18B20_READ_SCRATCHPAD  0xBE

/** @bref DS18B20温度变换
  * @param 无
  * @retval 无
  */
void DS18B20_convertT()
{
	OneWire_init();
	OneWire_sendByte(DS18B20_SIKP_ROM);
	OneWire_sendByte(DS18B20_CONVERT_T);
}

/** @bref DS18B20温度读取
  * @param 无
  * @retval 读取的温度
  */
float DS18B20_readT()
{
	unsigned char TLSB, TMSB;
	int temp;
	float T;
	
	OneWire_init();
	OneWire_sendByte(DS18B20_SIKP_ROM);
	OneWire_sendByte(DS18B20_READ_SCRATCHPAD);
	TLSB = OneWire_receiveByte();
	TMSB = OneWire_receiveByte();
	temp = (TMSB << 8) | TLSB;
	// 因为后四位是小数位，所以在原数基础上除以16得到温度
	T = temp / 16.0;
	
	return T;
}
